﻿using DreamCMS.BLL.Factory.Interface;
using DreamCMS.Entity;
using DreamCMS.WebCore.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace DreamCMS.DAL.DataAccessObject
{
    public class SiteSettingDAO:ISiteSettingDAO
    {
        /// <summary>
        /// 更新站点设置信息
        /// </summary>
        /// <param name="siteSetting">站点设置信息实体</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateSetting(SiteSettingInfo siteSetting)
        {
            string xmlStr=Serialize(siteSetting);
            string sql = "update [dm_SiteSetting] set [settingxml]=@Setting";
            long rel = AccessHelper.ExecuteNonQuery(CommandType.Text, sql, new OleDbParameter[] { AccessHelper.MakeParam("@Setting", OleDbType.VarWChar, 0, ParameterDirection.Input, xmlStr) });
            if(rel==1)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取站点设置实体
        /// </summary>
        /// <returns>返回站点设置信息实体</returns>
        public SiteSettingInfo GetSiteSetting()
        {
            string sql = "select top 1 [settingxml] from [dm_SiteSetting]";
            object rel = AccessHelper.ExecuteScalar(System.Data.CommandType.Text, sql);
            if(rel==null)
            {
                return new SiteSettingInfo();
            }
            string relStr = Convert.ToString(rel);
            object relObj = DeSerialize(typeof(SiteSettingInfo), relStr);
            if(relObj!=null)
            {
                return (SiteSettingInfo)relObj;
            }
            else
            {
                return new SiteSettingInfo();
            }

        }

        /// <summary>
        /// xml序列化成字符串
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>序列化后的xml字符串</returns>
        private static string Serialize(object obj)
        {
            string returnStr = "";

            XmlSerializer serializer = new XmlSerializer(obj.GetType());


            MemoryStream ms = new MemoryStream();
            XmlTextWriter xtw = null;
            StreamReader sr = null;
            try
            {
                xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
                xtw.Formatting = System.Xml.Formatting.Indented;
                serializer.Serialize(xtw, obj);
                ms.Seek(0, SeekOrigin.Begin);
                sr = new StreamReader(ms);
                returnStr = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (xtw != null)
                    xtw.Close();
                if (sr != null)
                    sr.Close();
                ms.Close();
            }
            return returnStr;

        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="s">要反序列号的XML字符串</param>
        /// <returns>反序列化后的对象实体</returns>
        private static object DeSerialize(Type type, string s)
        {
            byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
            try
            {
                XmlSerializer serializer = new XmlSerializer(type);

                return serializer.Deserialize(new MemoryStream(b));
            }
            catch
            {
                return null;
            }
        }
    }
}
